package com.atstudy.servlet;

import com.alibaba.fastjson2.JSON;
import com.atstudy.entites.Account;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/2/2 10:07
 * @copyright: 博为峰
 */
@WebServlet("/acc")
public class AccountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("AccountServlet");
        String name = req.getParameter("name");
        System.out.println("name = " + name);


        Account account = new Account(1, "zhangsan", 1000, new Date(), new Date());
        //将Java对象序列化为JSON
        String jsonString = JSON.toJSONString(account);

        resp.setContentType("text/html;charset=UTF-8");//解决响应乱码
        resp.getWriter().write(jsonString);//响应数据到客户端

    }
}
